import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QHBoxLayout, QPushButton


class Label(QLabel):

    def __init__(self, *args, **kwargs) -> None:
        super(Label, self).__init__(*args, **kwargs)
        # 0边距
        self.setContentsMargins(0, 0, 0, 0)


class Button(QPushButton):

    def __init__(self, *args, **kwargs) -> None:
        super(Button, self).__init__(*args, **kwargs)
        # 0边距
        self.setContentsMargins(0, 0, 0, 0)
        # 点击按钮之后马上清除焦点
        self.clicked.connect(self.clearFocus)


class FrameLess(QWidget):
    screen_width = 900
    screen_height = 800
    button_size = 50

    def __init__(self):
        super().__init__()
        self.initUi()

    def initUi(self):
        self.resize(self.screen_width, self.screen_height)  # 窗口大小
        self.setWindowTitle("Tetris")
        self.setWindowFlag(Qt.FramelessWindowHint)  # 无边框
        self.setWindowState(Qt.WindowActive)  # 活动窗口
        self.setWindowIcon(QIcon("../icon/captain_America.ico"))  # 设置图标
        self.background = Label(self)
        self.pixmap = QPixmap("../icon/background.png")
        self.background.setPixmap(self.pixmap)
        # 右上角的关闭按钮
        self.btn_close = Button(self)
        self.btn_close.setObjectName("closeButton")
        self.btn_close.setShortcut("ESC")  # 按钮热键esc
        self.btn_close.setToolTip("关闭")  # 悬停在按钮上的提示->关闭
        self.btn_close.move(self.screen_width - self.button_size, 5)  # 按钮的位置
        self.btn_close.clicked.connect(self.slot_clicked_close)

    def slot_clicked_close(self) -> None:
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = FrameLess()
    window.show()
    sys.exit(app.exec_())
